package com.jpro.framework.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class RequestUitls {
    /**
     * 获取当前会话中的HttpServletRequest
     *
     * @Title: getHttpServletRequest
     * @return: HttpServletRequest
     */
    public static HttpServletRequest getHttpServletRequest() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (sra != null) {
            return sra.getRequest();
        }
        return null;
    }

    public static String getRequestUrl(HttpServletRequest request) {
        String uri = request.getRequestURI();
        String ctx = request.getContextPath();
        if (StringUtils.isBlank(uri)) {
            return "";
        }
        if (StringUtils.isBlank(ctx)) {
            return uri;
        }
        return uri.substring(uri.indexOf(ctx) + ctx.length());
    }

}
